package model.turtle;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.geom.Point2D;

/**
 * LineSegment takes care of lines in the path of
 *      the Commandable object.
 *
 * @author Angelica Schwartz
 */
public class LineSegment extends PathSegment {
    /**
     * starting point for this line segment.
     */
    private Point2D point1;
    /**
     * ending point for this line segment.
     */
    private Point2D point2;
    /**
     * Color of this line segment.
     */
    private Color lineColor;
    /**
     * Width of this line segment.
     */
    private int lineWidth;
    /**
     * Design of this line segment.
     */
    private String lineDesign;
    /**
     * whether the pen was up or down when
     *      this line segment was drawn.
     */
    private boolean penDown;

    /**
     * Constructor.
     * @param p1 is starting point
     * @param p2 is ending point
     * @param c is Color of pen
     * @param width is width of pen
     * @param design is design of pen
     * @param down is whether pen is up or down
     */
    public LineSegment(Point2D p1, Point2D p2, Color c, int width,
                       String design, boolean down) {
        point1 = p1;
        point2 = p2;
        lineColor = c;
        lineWidth = width;
        lineDesign = design;
        penDown = down;
    }

    /**
     * method for painting the
     *      LineSegment.
     */
    @Override
    public void paint(Graphics2D pen) {
        if (penDown) {
            pen.setColor(lineColor);
            pen.setStroke(getStrokeDesign(lineWidth));
            pen.drawLine(((int) point1.getX()), ((int) point1.getY()),
                         ((int) point2.getX()), ((int) point2.getY()));
        }
    }

    /**
     * sets the stroke to match the desired line pattern.
     *
     * @param thickness is the pen width
     * @return BasicStroke for the correct pattern
     */
    private Stroke getStrokeDesign(int thickness) {
        if (lineDesign.equalsIgnoreCase(Commandable.DASHED_LINE_PATTERN)) {
            return new BasicStroke((float) thickness, BasicStroke.CAP_BUTT,
                       BasicStroke.JOIN_MITER, 10.0f, new float[]{10.0f}, 0.0f);
        }
        //TODO: add double line or other patterns
        return new BasicStroke((float) thickness);
    }
}
